/*
 * Canvas Game Engine - Canvas module
 *
 * version: 0.1
 *
 * author: Fabio Rotondo <fabio@rotondo.it>
 *
 */

function CGECanvas ( ch, name, width, height )
{
	this.ch = ch;

	this.name = name;

	this.canvas = document.createElement ( "canvas" );

	this.set_size ( width, height );

	this.ch._ready += 1;
	this.ctxt = this.canvas.getContext ( "2d" );

	this.color_clear = "#ff0000";
}

CGECanvas.prototype.render = function ( dest_div )
{
	if ( typeof ( dest_div ) == "string" )
		dest_div = document.getElementById ( dest_div );

	dest_div.appendChild ( this.canvas );

	CGE.set_canvas ( this );
};

CGECanvas.prototype.clear = function ( color )
{
	this.ctxt.clearRect ( 0, 0, this.w, this.h );
};

CGECanvas.prototype.save = function ()
{
	this.ctxt.save ();
};

CGECanvas.prototype.restore = function ()
{
	this.ctxt.restore ();
};

CGECanvas.prototype.clip = function ( x, y, w, h )
{
	this.ctxt.rect ( x, y, w, h );
	this.ctxt.clip ();
};

CGECanvas.prototype.set_size = function ( w, h )
{
	this.canvas.width = w;
	this.canvas.height = h;

	this.w = w;
	this.h = h;
};

function CGECanvasHandler ()
{
	this.canvases = {};

	this._tot = 0;
	this._ready = 0;
}

CGECanvasHandler.prototype.add = function ( name, w, h )
{
	var c;

	this._tot += 1;

	c = new CGECanvas ( this, name, w, h );
	this.canvases [ name ] = c;

	return c;
};


CGECanvasHandler.prototype.ready = function ()
{
	return this._tot == this._ready;
};

CGECanvasHandler.prototype.get = function ( name )
{
	return this.canvases [ name ];
};


CGE.register ( "canvas_handler", new CGECanvasHandler () );
